package cn.daniellee.plugin.pm.command;

import cn.daniellee.plugin.pm.PartnerMission;
import cn.daniellee.plugin.pm.core.MissionCore;
import cn.daniellee.plugin.pm.menu.MissionMenu;
import cn.daniellee.plugin.pm.model.Mission;
import cn.daniellee.plugin.pm.model.PlayerData;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.Map;

public class MissionCommand implements CommandExecutor {

	@Override
	public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
		// 玩家无参数使用则打开任务菜单
		if (strings.length == 0 && commandSender instanceof Player player) {
			PlayerData playerData = PartnerMission.getInstance().getStorage().getPlayerByName(player.getName());
			player.openInventory(MissionMenu.generate(player, playerData.isByTimes(), playerData.isShowOffline(), 1));
		} else if (strings.length != 0) {
			// 邀请玩家
			if (strings[0].equalsIgnoreCase("invite") && strings.length > 1 && commandSender instanceof Player player) {
				Player invitee = Bukkit.getPlayer(strings[1]);
				MissionCore.invitePlayer(player, invitee);
			// 接受邀请
			} else if (strings[0].equalsIgnoreCase("accept") && strings.length > 1 && commandSender instanceof Player player) {
				Player inviter = Bukkit.getPlayer(strings[1]);
				MissionCore.beginMission(inviter, player);
			// 清除资料
			} else if (strings[0].equalsIgnoreCase("clear") && strings.length > 1 && commandSender.hasPermission("mission.command.clear")) {
				PlayerData playerData = PartnerMission.getInstance().getStorage().getPlayerByName(strings[1]);
				Map<String, Object> fields = new HashMap<>();
				fields.put("contact", "");
				fields.put("introduction", "[]");
				PartnerMission.getInstance().getStorage().updatePlayer(playerData.getName(), fields);
				commandSender.sendMessage((PartnerMission.getInstance().getPrefix() + PartnerMission.getInstance().getConfig().getString("message.clear-success", "&eClear player data successfully.")).replace("&", "§"));
			// 取消任务
			} else if (strings[0].equalsIgnoreCase("cancel") && strings.length > 1 && commandSender.hasPermission("mission.command.cancel")) {
				PlayerData playerData = PartnerMission.getInstance().getStorage().getPlayerByName(strings[1]);
				if (MissionCore.missions.removeIf(item -> item.getInviter().equals(playerData.getName()) || item.getInvitee().equals(playerData.getName()))) {
					commandSender.sendMessage((PartnerMission.getInstance().getPrefix() + PartnerMission.getInstance().getConfig().getString("message.cancel-success", "&eCancel the task successfully.")).replace("&", "§"));
				} else commandSender.sendMessage((PartnerMission.getInstance().getPrefix() + PartnerMission.getInstance().getConfig().getString("message.no-mission", "&eThe player is not performing a mission.")).replace("&", "§"));
			// 重载插件
			} else if (strings[0].equalsIgnoreCase("reload") && commandSender.hasPermission("mission.command.reload")) {
				PartnerMission.getInstance().reloadConfig();
				if (PartnerMission.getInstance().loadConfig()) {
					commandSender.sendMessage((PartnerMission.getInstance().getPrefix() + PartnerMission.getInstance().getConfig().getString("message.reload-success", "&eReload has been successful.")).replace("&", "§"));
				}
				// 发送帮助
			} else sendHelp(commandSender);
		} else commandSender.sendMessage((PartnerMission.getInstance().getPrefix() + PartnerMission.getInstance().getConfig().getString("message.player-only", "&eThe console cannot open the GUI menu.")).replace("&", "§"));
		return true;
	}

	private void sendHelp(CommandSender commandSender) {
		commandSender.sendMessage(("&m&6---&m&a--------&3 : " + PartnerMission.getInstance().getConfig().getString("prompt-prefix", "&6PartnerMission") + "&3 : &m&a--------&m&6---").replace("&", "§"));

		String mainText =  PartnerMission.getInstance().getConfig().getString("help.main", "&eOpen the partner mission GUI menu.").replace("&", "§");
		TextComponent mainHelp = new TextComponent("/pm  " + mainText);
		mainHelp.setColor(ChatColor.GRAY);
		mainHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/pm"));
		mainHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(mainHelp).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(mainHelp);

		String inviteText =  PartnerMission.getInstance().getConfig().getString("help.invite", "&eInvite player to perform missions.").replace("&", "§");
		TextComponent inviteHelp = new TextComponent("/pm invite <player>  " + inviteText);
		inviteHelp.setColor(ChatColor.GRAY);
		inviteHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/pm invite "));
		inviteHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(inviteText).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(inviteHelp);

		String acceptText =  PartnerMission.getInstance().getConfig().getString("help.accept", "&eAccept invitations from player.").replace("&", "§");
		TextComponent acceptHelp = new TextComponent("/pm accept <player>  " + acceptText);
		acceptHelp.setColor(ChatColor.GRAY);
		acceptHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/pm accept "));
		acceptHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(acceptText).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(acceptHelp);

		String clearText =  PartnerMission.getInstance().getConfig().getString("help.clear", "&eClear player data.").replace("&", "§");
		TextComponent clearHelp = new TextComponent("/pm clear <player>  " + clearText);
		clearHelp.setColor(ChatColor.GRAY);
		clearHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/pm clear "));
		clearHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(clearText).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(clearHelp);

		String cancelText =  PartnerMission.getInstance().getConfig().getString("help.cancel", "&eCancel a player's mission.").replace("&", "§");
		TextComponent cancelHelp = new TextComponent("/pm cancel <player>  " + cancelText);
		cancelHelp.setColor(ChatColor.GRAY);
		cancelHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/pm cancel "));
		cancelHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(cancelText).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(cancelHelp);

		String reloadText = PartnerMission.getInstance().getConfig().getString("help.reload", "&eReload configuration.").replace("&", "§");
		TextComponent reloadHelp = new TextComponent("/pm reload  " + reloadText);
		reloadHelp.setColor(ChatColor.GRAY);
		reloadHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/pm reload"));
		reloadHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(reloadText).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(reloadHelp);
	}
}
